package com.chj.comp.service.demo1.controller;

import com.chj.comp.common.core.util.MinioUtil;
import com.chj.comp.common.core.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author ：cuihui
 * @date ：Created in 2023/1/13
 * @description：TODO
 * @modified By：
 */
@RestController
@RequestMapping("/file")
@Api(tags = "文件接口")
@Slf4j
public class MinioTestController {

    @Autowired
    private MinioUtil minioUtil;

    /**
     * 上传文件
     *
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation(value = "上传文件")
    public Result upload(@ApiParam(value = "存储桶", required = true) @RequestParam(name = "bucketName") String bucketName,
                         @ApiParam(value = "前缀：r-只读 w-只写 a-读写", required = true) @RequestParam(name = "prefix", defaultValue = "r") String prefix,
                         @ApiParam(value = "文件", required = true) @RequestParam(name = "file") MultipartFile file) {
        return minioUtil.uploadFile(bucketName, prefix, file);
    }

    /**
     * 删除文件
     *
     * @param bucketName
     * @param fileName
     * @return
     */
    @PostMapping("/delete")
    @ApiOperation(value = "删除文件")
    public Result delete(@ApiParam(value = "存储桶", required = true) @RequestParam(name = "bucketName") String bucketName,
                         @ApiParam(value = "文件名称", required = true) @RequestParam(name = "fileName") String fileName) {
        return minioUtil.deleteFile(bucketName, fileName);
    }
}
